package org.mclll.xwangplugin.mcmmoCustom.poiont.config;

import com.gmail.nossr50.config.skills.alchemy.PotionConfig;
import com.gmail.nossr50.datatypes.skills.alchemy.AlchemyPotion;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.inventory.ItemStack;
import org.mclll.xwangplugin.log.GlobalLog;
import org.mclll.xwangplugin.log.LogUtil;
import org.mclll.xwangplugin.log.LoggerFactory;

import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author: geduo
 * @date: 2023/10/17 17:01
 */
public class ProbBrewerConfig {
    public static Boolean debugMode;
    public static final String CONFIG_FILE_NAME = "point.yml";
    public static final LogUtil LOG = LoggerFactory.get(GlobalLog.class);
    private static Map<ItemStack, Map<Material, List<ProbBrewer>>> config;

    public static void load(File file, PotionConfig potionConfig) {
        String root = "ProbBrewer";
        YamlConfiguration configFile = YamlConfiguration.loadConfiguration(new File(file, CONFIG_FILE_NAME));
        debugMode = configFile.getBoolean("Debug");
        ConfigurationSection rootConfig = configFile.getConfigurationSection(root);
        config = Maps.newHashMap();
        for (String pointId : rootConfig.getKeys(false)) {
            AlchemyPotion potion = potionConfig.getPotion(pointId);
            if (potion == null) {
                LOG.info("未知的药水标识符号：{0}", pointId);
                return;
            }
            ConfigurationSection pointOption = rootConfig.getConfigurationSection(pointId);
            Set<String> catalystList = pointOption.getKeys(false);
            Map<Material, List<ProbBrewer>> catalystToProb = Maps.newHashMap();
            for (String catalystId : catalystList) {
                List<ProbBrewer> pointProbList = Lists.newArrayList();
                Material catalyst = null;
                try {
                    catalyst = Material.valueOf(catalystId);
                } catch (Exception e) {
                    LOG.info("未知的催化剂：{0}", catalystId);
                    return;
                }
                for (Map<?, ?> map : pointOption.getMapList(catalystId)) {
                    Double prob = Double.valueOf(String.valueOf(map.get("prob")));
                    String subPointId = (String) map.get("pointId");
                    AlchemyPotion subPotion = potionConfig.getPotion(subPointId);
                    if (subPotion == null) {
                        LOG.info("未知的药水标识符号：{0}", subPointId);
                        return;
                    }
                    pointProbList.add(new ProbBrewer(prob, subPotion.toItemStack(1)));
                }
                catalystToProb.put(catalyst, pointProbList);
            }
            config.put(potion.toItemStack(1), catalystToProb);
        }
        LOG.info("药水概率表转换表加载完毕");
    }

    public static List<ProbBrewer> get(ItemStack inputPoint, Material type) {
        for (ItemStack key : config.keySet()) {
            if (key.isSimilar(inputPoint)) {
                return config.get(key).getOrDefault(type, Lists.newArrayList());
            }
        }
        return null;
    }

    @Data
    @AllArgsConstructor
    public static class ProbBrewer {
        private Double prob;
        private ItemStack point;
    }
}
